//
//  ViewController.m
//  UITextView
//
//  Created by gzjd on 2017/5/13.
//  Copyright © 2017年 gzjd. All rights reserved.
//

#import "ViewController.h"
#import "Masonry.h"
#import "SecondController.h"
#import "XNTabBarController.h"
#import "FirstViewController.h"
@interface ViewController ()

@end


@implementation ViewController{
    UITextView *_textView;
    UITextField *_textField;
}
-(void)dealloc{
    [_textView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor colorWithRed:245/255.0 green:245/255.0 blue:245/255.0 alpha:1]];
    UIImageView* imgLogin = [UIImageView new];
    /*initWithFrame:CGRectMake(mainSize.width / 2 - 211 / 2, 100, 211, 109)];*/
    imgLogin.image = [UIImage imageNamed:@"aa"];
    imgLogin.layer.masksToBounds = YES;
    [self.view addSubview:imgLogin];
    [imgLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100,110));
        make.centerX.equalTo(self.view);
        make.top.equalTo(self.view.mas_top).with.offset(110);
    }];
    
    UITextField * txtUser = [UITextField new];
    txtUser.backgroundColor=[UIColor whiteColor];
    txtUser.layer.cornerRadius = 0;
    txtUser.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    txtUser.layer.borderWidth = 0.5;
    txtUser.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 6, 0)];
    txtUser.leftViewMode = UITextFieldViewModeAlways;
    txtUser.leftView.backgroundColor=[UIColor blueColor];
    txtUser.placeholder = @"请输入用户名...";
    txtUser.keyboardType = UIKeyboardTypeAlphabet;   //数字键盘
    txtUser.keyboardAppearance = UIKeyboardAppearanceDefault;
    [self.view addSubview:txtUser];
    
    
    UITextField * txtPasswd = [UITextField new];
    txtPasswd.backgroundColor=[UIColor whiteColor];
    txtPasswd.layer.cornerRadius = 0;
    txtPasswd.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    txtPasswd.layer.borderWidth = 0.5;
    txtPasswd.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 6, 0)];
    txtPasswd.leftViewMode = UITextFieldViewModeAlways;
    txtPasswd.leftView.backgroundColor=[UIColor blueColor];
    txtPasswd.placeholder = @"请输入密码...";
    txtPasswd.returnKeyType = UIReturnKeyDefault;//return键的类型
    txtPasswd.keyboardType = UIKeyboardTypeAlphabet;//键盘类型
    txtPasswd.textAlignment = NSTextAlignmentLeft; //文本显示的位置默认为居左
    [self.view addSubview:txtPasswd];
    
    
    //登录按钮
    UIButton *btnLogin=[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [btnLogin setTitle:@"登录" forState:UIControlStateNormal];
    btnLogin.titleLabel.font = [UIFont systemFontOfSize:20];
    [btnLogin setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btnLogin.backgroundColor = [UIColor blueColor];
    btnLogin.layer.cornerRadius = 2.0;//2.0是圆角的弧度，根据需求自己更改
    [btnLogin addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btnLogin];
    
    
    
    [btnLogin mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(txtPasswd.mas_bottom).with.offset(50);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.right.equalTo(self.view.mas_right).with.offset(-30);
        make.height.mas_equalTo(50);
    }];

    
    [txtUser mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(imgLogin.mas_bottom).with.offset(50);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.right.equalTo(self.view.mas_right).with.offset(-30);
        make.height.mas_equalTo(50);
    }];
    
    [txtPasswd mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(txtUser.mas_bottom).with.offset(-0.5);
        make.left.equalTo(self.view.mas_left).with.offset(30);
        make.right.equalTo(self.view.mas_right).with.offset(-30);
        make.height.mas_equalTo(50);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)btnClick:(UIButton*)button
{
    
    UITabBarController *tb=[[UITabBarController alloc]init];
    [[UITabBar appearance] setBarTintColor:[UIColor whiteColor]];
    [UITabBar appearance].translucent = NO;
    
    UINavigationBar *navBar = [UINavigationBar appearance];
    navBar.barTintColor = [UIColor redColor];
    NSDictionary *dict = @{NSForegroundColorAttributeName : [UIColor whiteColor]};
    [navBar setTitleTextAttributes:dict];
    
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor lightGrayColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:                                                         [NSDictionary dictionaryWithObjectsAndKeys:[UIColor greenColor],NSForegroundColorAttributeName, nil]forState:UIControlStateSelected];
    
    
    FirstViewController *fv=[[FirstViewController alloc]init];
    fv.tabBarItem.title=@"消息";
        fv.tabBarItem.image=[UIImage imageNamed:@"tab_discover_normal"];
        fv.tabBarItem.badgeValue=@"123";
        fv.tabBarItem.selectedImage=[[UIImage imageNamed:@"tab_discover_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
//    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:fv];
//    
//    nav.tabBarItem.title=@"消息";
//    nav.tabBarItem.image=[UIImage imageNamed:@"tab_discover_normal"];
//    nav.tabBarItem.badgeValue=@"123";
//    nav.tabBarItem.selectedImage=[[UIImage imageNamed:@"tab_discover_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    
//    UIViewController *c1=[[UIViewController alloc]init];
//    c1.view.backgroundColor=[UIColor grayColor];
//    c1.tabBarItem.title=@"消息";
//    c1.tabBarItem.image=[UIImage imageNamed:@"tab_discover_normal"];
//    c1.tabBarItem.badgeValue=@"123";
    
    
   // c1.tabBarItem.selectedImage=[[UIImage imageNamed:@"tab_discover_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIViewController *c2=[[UIViewController alloc]init];
    c2.view.backgroundColor=[UIColor brownColor];
    c2.tabBarItem.title=@"联系人";
    c2.tabBarItem.image=[[UIImage imageNamed:@"tab_me_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    c2.tabBarItem.selectedImage=[[UIImage imageNamed:@"tab_me_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

    UIViewController *c3=[[UIViewController alloc]init];
    c3.tabBarItem.title=@"动态";
    c3.tabBarItem.image=[[UIImage imageNamed:@"tab_message_normal"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    c3.tabBarItem.selectedImage=[[UIImage imageNamed:@"tab_message_selected"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    ///c3.tabBarItem.imageInsets = UIEdgeInsetsMake(8, 5, 2, 5);
//    UIViewController *c4=[[UIViewController alloc]init];
//    c4.tabBarItem.title=@"设置";
//    c4.tabBarItem.image=[UIImage imageNamed:@"tab_me_normal"];
    
    //c.2第二种方式
    tb.viewControllers=@[fv,c2,c3];
    //XNTabBarController *xc= [[XNTabBarController alloc]init];
    tb.modalTransitionStyle=UIModalTransitionStyleCrossDissolve;
  
    //SecondController *sc=[[SecondController alloc]init];
    //[self presentModalViewController:sc animated:YES];
   
    [self presentViewController:tb animated: YES completion:nil];
    //[sc release];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    return YES;
    
}
- (void)textViewDidChange:(UITextView *)textView{
    NSLog(@"didchange!!!");
}
- (void)textViewDidChangeSelection:(UITextView *)textView{
    NSLog(@"DidchangeSelection");
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //回收键盘，两种方式
    //UITextView *textView = (UITextView*)[self.view viewWithTag:1001];
    //[textView resignFirstResponder];
    [self.view endEditing:YES];
    NSLog(@"touch");
}
//可以实现多行输入的文本框，基本属性与UITextField相似，可以输入多行，可以滚动。
//UITextView还有个代理方式
//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
//
//可以控制输入文字的数量，较为常用

#pragma mark UITextView的代理方法
//
////是否可以开始编辑
//- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
//{
//    NSLog(@"%s",__func__);
//    return YES;
//}
//
////是否可以结束编辑
//- (BOOL)textViewShouldEndEditing:(UITextView *)textView
//{
//    NSLog(@"%s",__func__);
//    return YES;
//}
//
////已经开始编辑
//- (void)textViewDidBeginEditing:(UITextView *)textView
//{
//    NSLog(@"%s",__func__);
//}
////已经结束编辑
//- (void)textViewDidEndEditing:(UITextView *)textView
//{
//    NSLog(@"%s",__func__);
//}
//
////内容变化
//- (void)textViewDidChange:(UITextView *)textView
//{
//    NSLog(@"%s",__func__);
//}
////光标变化
//- (void)textViewDidChangeSelection:(UITextView *)textView
//{
//    NSLog(@"%s",__func__);
//}
//
////当前输入的位置，当前输入的文字，是否可以继续输入
//- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
//{
//    NSLog(@"length = %ld,location = %ld",range.length,range.location);
//    NSLog(@"text = %@",text);
//    return YES;
//}
////下面这俩不好理解，大概是验证url和文件名后缀的
////Asks the delegate if the specified text view should allow user interaction with the given URL in the given range of text.
//- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0)
//{
//    NSLog(@"url= %@",URL.host);
//    NSLog(@"url touch");
//    return YES;
//}
//- (BOOL)textView:(UITextView *)textView shouldInteractWithTextAttachment:(NSTextAttachment *)textAttachment inRange:(NSRange)characterRange NS_AVAILABLE_IOS(7_0)
//{
//    return YES;
//}
//二、键盘回收与键盘遮挡输入框
//使用输入文本框时，经常出现一个问题，弹出的键盘挡住了文本框，这时可以通过移动输入框的位置来避免这样的情况。
//思路比较简单，注册通知来监听键盘的弹出和消失事件，再实现对应的方法，在键盘弹出或者消失的时候，改变原本视图的frame
//使视图向上或者向下移动一个键盘的高度，键盘就不会遮挡住视图了
//
//首先注册通知简体键盘弹出事件
//
////注册通知,监听键盘弹出事件
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:)
//                                             name:UIKeyboardWillShowNotification object:nil];
//
////注册通知,监听键盘消失事件
//[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden)
//                                             name:UIKeyboardDidHideNotification object:nil];
//键盘一旦弹出或者消失就触发方法对输入框进行移动
//
//// 键盘弹出时调用方法
//-(void)keyboardDidShow:(NSNotification *)notification
//
////键盘消失时调用
//-(void)keyboardDidHidden
//再设置一个触摸事件，触摸空白处可以收回键盘
//
////点击屏幕空白处
//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    //回收键盘，两者方式
//    //UITextView *textView = (UITextView*)[self.view viewWithTag:1001];
//    //[textView resignFirstResponder];
//    [self.view endEditing:YES];
//    NSLog(@"touch");
//}
//完整代码：
//
//#import "FirstViewController.h"
//
//@implementation FirstViewController
//
//-(void)viewDidLoad
//{
//    [super viewDidLoad];
//    
//#pragma mark UITextView
//    
//    //    UITextField：
//    //    继承UIControl,只能输入一行，不可以滚动，可以设置提醒文字。
//    //    有return代理方法和clearButtonMode
//    
//    //    UITextView：
//    //    能输入多行，可以滚动，不可以设置提醒文字。
//    
//    UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(10, 10, 280, 80)];//初始化
//    
//    textView.backgroundColor=[UIColor colorWithRed:0.21 green:0.71 blue:0.51 alpha:0.5]; //背景色
//    
//    textView.scrollEnabled = YES;    //当文字超过视图的边框时是否允许滑动，默认为“YES”
//    
//    textView.editable = YES;        //是否允许编辑内容，默认为“YES”
//    
//    textView.font=[UIFont fontWithName:@"Arial" size:18.0]; //设置字体名字和字体大小;
//    
//    textView.returnKeyType = UIReturnKeyDefault;//return键的类型
//    
//    textView.keyboardType = UIKeyboardTypeDefault;//键盘类型
//    
//    textView.textAlignment = NSTextAlignmentLeft; //文本显示的位置默认为居左
//    
//    textView.dataDetectorTypes = UIDataDetectorTypeAll; //显示数据类型的连接模式（如电话号码、网址、地址等）
//    
//    textView.textColor = [UIColor blackColor];
//    
//    textView.delegate = self;    //设置代理方法的实现类
//    
//    textView.text = @"UITextView";//设置显示的文本内容
//    
//    [textView.layer setCornerRadius:10]; //设置圆角
//    
//    textView.tag = 1001; //设置tag值
//    
//    //添加键盘的监听事件
//    
//    //注册通知,监听键盘弹出事件
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:)
//                                                 name:UIKeyboardDidShowNotification object:nil];
//    
//    //注册通知,监听键盘消失事件
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden)
//                                                 name:UIKeyboardDidHideNotification object:nil];
//    
//    //在view中添加一个子view，设置此子view的tag值为1000，在此view上添加一个textView和一个发送按钮，
//    //如下图；我们要达到textView的键盘弹出时，整个View往上平移，键盘消失，view往下平移的效果，模拟发送短信的界面。
//    UIView *keyView = [[UIView alloc]initWithFrame:CGRectMake(0, 567, 375, 100)];
//    keyView.backgroundColor = [UIColor colorWithRed:0.2 green:0.3 blue:0.6 alpha:0.5];
//    keyView.tag = 1000;
//    
//    [keyView addSubview:textView];
//    
//    [self.view addSubview:keyView];
//    
//}

//#pragma mark 实现监听到键盘变化时的触发的方法
//
//// 键盘弹出时
//-(void)keyboardDidShow:(NSNotification *)notification
//{
//    //获取键盘高度
//    NSValue *keyboardObject = [[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey];
//    
//    NSLog(@"%@",keyboardObject);
//    
//    CGRect keyboardRect;
//    
//    [keyboardObject getValue:&keyboardRect];
//    
//    //得到键盘的高度
//    //CGRect keyboardRect = [[notification.userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]CGRectValue];
//    
//    // 取得键盘的动画时间，这样可以在视图上移的时候更连贯
//    double duration = [[notification.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] doubleValue];
//    NSLog(@"%f",duration);
//    //调整放置有textView的view的位置
//    
//    //设置动画
//    [UIView beginAnimations:nil context:nil];
//    
//    //定义动画时间
//    [UIView setAnimationDuration:duration];
//    [UIView setAnimationDelay:0];
//    
//    //设置view的frame，往上平移
//    [(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, self.view.frame.size.height-keyboardRect.size.height-100, 375, 100)];
//    
//    //提交动画
//    [UIView commitAnimations];
//    
//}
//
////键盘消失时
//-(void)keyboardDidHidden
//{
//    //定义动画
//    //[UIView beginAnimations:nil context:nil];
//    // [UIView setAnimationDuration:0.25];
//    
//    //设置view的frame，往下平移
//    [(UIView *)[self.view viewWithTag:1000] setFrame:CGRectMake(0, 567, 375, 100)];
//    // [UIView commitAnimations];
//}
//
////点击屏幕空白处
//-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
//{
//    //回收键盘，两种方式
//    //UITextView *textView = (UITextView*)[self.view viewWithTag:1001];
//    //[textView resignFirstResponder];
//    [self.view endEditing:YES];
//    NSLog(@"touch");
//}
//
//@end
//效果图
//
//
//
//后面又一次要在tableView里面加textView，不好找的textView去resignKeyBoard，就使用一个可以得到的textView 去becomeFirst，然后再resignFirst
//
//还有直接获得第一响应者的方法
//UIWindow keyWindow = [[UIApplication sharedApplication] keyWindow];
//UIView firstResponder = [keyWindow performSelector:@selector(firstResponder)];
//[firstResponder resignFirstResponder];
//
//
//
//转载：http://www.jianshu.com/p/1bf3407b15f7
//
//你好，如若我写的博客能帮到你解决你遇到的问题，请留下认可的足迹，方便其他同行也能解决他们遇到的问题。 如若我写的博客有缺陷，请您也及时发表一下你的见解，好帮到其他人，共勉吧！！ 或者在看我的博客中，遇到了不明白的，您也可以及时评论，我会如实和你交流。 thx
//
//



@end
